# -*- tcl -*-
# # ## ### ##### ######## ############# #####################
##
# Test suite for Tcl/Hwloc binding.
# Topology. Node/CPU set conversion.

# # ## ### ##### ######## ############# #####################
## Requirements

package require Tcl 8.5

if {[catch {package require tcltest 2}]} {
    puts stderr "Skipping tests in [info script].  tcltest 2 required."
    return
}

# # ## ### ##### ######## ############# #####################

test hwloc-topology-convert-1.0 "node/cpu conversion, wrong#args" -setup {
    hwloc create T
} -body {
    T convert
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T convert option ?-strict? bitmap"}

test hwloc-topology-convert-1.1 "node/cpu conversion, wrong#args" -setup {
    hwloc create T
} -body {
    T convert X
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T convert option ?-strict? bitmap"}

test hwloc-topology-convert-1.2 "node/cpu conversion, wrong#args" -setup {
    hwloc create T
} -body {
    T convert 2nodeset Y Z
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T convert option ?-strict? bitmap"}

test hwloc-topology-convert-1.3 "node/cpu conversion, wrong#args" -setup {
    hwloc create T
} -body {
    T convert 2cpuset -strict Y Z
} -cleanup {
    T destroy
} -returnCodes error -result {wrong # args: should be "T convert option ?-strict? bitmap"}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-convert-2.0 "node/cpu conversion, bad option" -setup {
    hwloc create T
} -body {
    T convert foo X
} -cleanup {
    T destroy
} -returnCodes error -result {bad option "foo": must be 2cpuset or 2nodeset}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-convert-3.0 "node/cpu conversion, bad bitmap" -setup {
    hwloc create T
} -body {
    T convert 2nodeset X
} -cleanup {
    T destroy
} -returnCodes error -result {Expected bitmap but got "X"}

test hwloc-topology-convert-3.1 "node/cpu conversion, bad bitmap" -setup {
    hwloc create T
} -body {
    T convert 2cpuset X
} -cleanup {
    T destroy
} -returnCodes error -result {Expected bitmap but got "X"}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-topology-convert-4.1 "node -> cpu conversion, non-numa, empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2cpuset {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-4.2 "node -> cpu conversion, non-numa, non empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2cpuset 0
} -cleanup {
    T destroy
} -result 0-

test hwloc-topology-convert-4.3 "node -> cpu conversion, strict, non-numa, empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2cpuset -strict {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-4.4 "node -> cpu conversion, strict, non-numa, empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2cpuset -strict 0
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-4.5 "cpu -> node conversion, non-numa, empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2nodeset {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-4.6 "cpu -> node conversion, non-numa, not empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2nodeset 0
} -cleanup {
    T destroy
} -result 0-

test hwloc-topology-convert-4.7 "cpu -> node conversion, strict, non-numa, empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2nodeset -strict {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-4.8 "cpu -> node conversion, strict, non-numa, not empty" -setup {
    hwloc create T -synthetic pu:4
} -body {
    T convert 2nodeset -strict 0
} -cleanup {
    T destroy
} -result {}

# - -- --- ----- -------- ------------- ---------------------
# numa node 0 => cpus 0-3
# cpu 0 => numa node 0

test hwloc-topology-convert-5.1 "node -> cpu conversion, numa, empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2cpuset {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-5.2 "node -> cpu conversion, numa, non empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2cpuset 0
} -cleanup {
    T destroy
} -result 0-3

test hwloc-topology-convert-5.3 "node -> cpu conversion, strict, numa, empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2cpuset -strict {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-5.4 "node -> cpu conversion, strict, numa, empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2cpuset -strict 0
} -cleanup {
    T destroy
} -result 0-3

test hwloc-topology-convert-5.5 "cpu -> node conversion, numa, empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2nodeset {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-5.6 "cpu -> node conversion, numa, not empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2nodeset 0
} -cleanup {
    T destroy
} -result 0

test hwloc-topology-convert-5.7 "cpu -> node conversion, strict, numa, empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2nodeset -strict {}
} -cleanup {
    T destroy
} -result {}

test hwloc-topology-convert-5.8 "cpu -> node conversion, strict, numa, not empty" -setup {
    hwloc create T -synthetic {node:3 pu:4}
} -body {
    T convert 2nodeset -strict 0
} -cleanup {
    T destroy
} -result 0

# # ## ### ##### ######## ############# #####################
tcltest::cleanupTests
